//
//  XSSearchViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/27.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSSearchViewController.h"

@interface XSSearchViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray * titleArr;
    NSInteger  lastIndex;
    
    NSArray * normalArr;
    NSArray * slectArr;
}
@end

@implementation XSSearchViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden =YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    XSView * view =[[XSView alloc]initWithBool:NO withString:self.Title];
    [self.view addSubview:view];
    UIButton * btn1 =(UIButton *)[view viewWithTag:1];
    [btn1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    titleArr =@[@"新品",@"热销",@"价格"];
    normalArr =@[@"yuike_itemgroup_arrow_gray",@"yuike_itemgroup_arrow_gray",@"yuike_itemgroup_arrow2_gray"];
    
    slectArr =@[@"yuike_itemgroup_arrow_black",@"yuike_itemgroup_arrow_black",@"yuike_itemgroup_arrow2_blackdown",@"yuike_itemgroup_arrow2_blackup"];
    [self createButtonView];
    UICollectionViewFlowLayout * layout =[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize =CGSizeMake(self.view.bounds.size.width/2-20, 300);
    
    layout.sectionInset =UIEdgeInsetsMake(_btnView.bounds.size.height+64, 10, 0, 10);
    
    layout.minimumInteritemSpacing =5;
    layout.minimumLineSpacing =10;
    
    UICollectionView * collection =[[UICollectionView alloc]initWithFrame:CGRectMake(0, _btnView.frame.size.height+_btnView.frame.origin.y, _btnView.frame.size.width, self.view.bounds.size.height-50) collectionViewLayout:layout];
    collection.backgroundColor =[UIColor whiteColor];
    collection.delegate =self;
    collection.dataSource =self;
    [self.view addSubview:collection];
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
}
-(void)createButtonView
{
    _btnView =[[UIView alloc]init];
    _btnView.frame =CGRectMake(0, 64, self.view.frame.size.width, 50);
    _btnView.backgroundColor =backColor;
    [self.view addSubview:_btnView];
    UIImageView * line =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _btnView.frame.size.width, 2)];
    line.image =[UIImage imageNamed:@"line_activity"];
    [_btnView addSubview:line];
    UIImageView * line2 =[[UIImageView alloc]initWithFrame:CGRectMake(0, _btnView.frame.size.height, _btnView.frame.size.width, 2)];
    line2.image =[UIImage imageNamed:@"line_activity"];
    [_btnView addSubview:line2];
    
    
    UIButton * lastBtn;
    for (int i = 0; i < 3; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag =i+1;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_btnView addSubview:button];
        
        UILabel * lab =[[UILabel alloc]init];
        lab.font =[UIFont systemFontOfSize:13];
        lab.text =titleArr[i];
        [button addSubview:lab];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(0);
            make.bottom.offset(0);
            
            if (lastBtn) {
                //存在的话说明第二个按钮起...
                make.left.equalTo(lastBtn.mas_right).with.offset(0);
                make.width.equalTo(lastBtn.mas_width);
            }
            else
            {
                //不存在说明第一个
                make.left.offset(0);
            }
            if (i==2) {
                make.right.offset(0);
            }
            
        }];

        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(10);
            make.bottom.offset(10);
            make.centerX.equalTo(button.mas_centerX).with.offset(20);
            make.width.equalTo(@60);
        }];
        lastBtn =button;
        
        UIImageView * imageView =[[UIImageView alloc]init];
        imageView.center =CGPointMake(lab.frame.size.width+lab.frame.origin.x, lab.center.y);
//        imageView.image =
        imageView.tag =i+20;
        
        [button addSubview:imageView];
        
        if (i==0)
        {
            lab.textColor =[UIColor magentaColor];
            imageView.image =[UIImage imageNamed:slectArr[i]];
        }
        else
        {
            lab.textColor =[UIColor blackColor];
            imageView.image =[UIImage imageNamed:normalArr[i]];
        }
        
        
        imageView.userInteractionEnabled =NO;
        lab.userInteractionEnabled = NO;
    }

    
}
-(void)btnClick:(UIButton *)btn
{
    NSInteger current =btn.tag-1;
    
    //切换界面
    [self selectBtnWithNSInterge:current];
}
-(void)hightiliImageWith:(NSInteger)current
{
    UIImageView * lastImage =(UIImageView *)[_btnView viewWithTag:lastIndex+20];
    lastImage.image =[UIImage imageNamed:normalArr[lastIndex]];
    
    UIImageView * imageView =(UIImageView *)[_btnView viewWithTag:current+20];
    imageView.image =[UIImage imageNamed:slectArr[current]];
   
}
-(void)selectBtnWithNSInterge:(NSInteger)index
{
    
    if (lastIndex ==index)
    {
        return;
    }
    if (index !=0)
    {
        [self hightiliImageWith:index];
    }
    
    switch (index) {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            
        }
            break;
        case 2:
        {
            
            
        }
            break;
        default:
            break;
    }
    
    //记录当前点击的按钮
    lastIndex =index;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor =[UIColor whiteColor];
    
    UIImageView * imageView1 =(UIImageView *)[cell.contentView viewWithTag:cell.tag+10];
    if (imageView1==nil)
    {
        imageView1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width/2-10, 240)];
        imageView1.tag =cell.tag+10;
        
        [cell.contentView addSubview:imageView1];
    }
    
    UILabel * lab1 =(UILabel *)[cell.contentView viewWithTag:cell.tag +200];
    if (lab1 ==nil)
    {
        lab1 =[[UILabel alloc]initWithFrame:CGRectMake(0, 240, self.view.bounds.size.width/2-10, 50)];
        lab1.tag =cell.tag +200;
        
        lab1.font =[UIFont systemFontOfSize:14];
        lab1.numberOfLines =0;
        
        [cell.contentView addSubview:lab1];
    }
    UILabel * lab2 =(UILabel *)[cell.contentView viewWithTag:cell.tag +500];
    if (lab2 ==nil)
    {
        lab2 =[[UILabel alloc]initWithFrame:CGRectMake(0, 280, self.view.bounds.size.width/2-10, 20)];
        lab2.tag =cell.tag +500;
        
        lab2.textColor =[UIColor magentaColor];
        [cell.contentView addSubview:lab2];
    }

    
    return cell;
}
-(void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
